﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ThemSanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void lvCountries_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        DropDownList ddl = e.Item.FindControl("ddlLoaiTLCha") as DropDownList;
        if (ddl != null)
        {
            e.Values["IDLoaiTL"] = (Convert.ToInt32(ddl.SelectedValue));
        }

        DropDownList ddl2 = e.Item.FindControl("ddlTaiLieuMoi") as DropDownList;
        if (ddl2 != null)
        {
            e.Values["TaiLieuMoi"] = (Convert.ToInt32(ddl2.SelectedValue));
        }

        DropDownList ddl3 = e.Item.FindControl("ddlTrangThai") as DropDownList;
        if (ddl3 != null)
        {
            e.Values["TrangThai"] = (Convert.ToInt32(ddl3.SelectedValue));
        }

        FileUpload fuPicture = e.Item.FindControl("fuPicture") as FileUpload;
        if (fuPicture.HasFile)
        {
            if (CheckFileType(fuPicture.FileName))
            {
                string file_post_url = Server.MapPath("..") + "/Image/Sach/" + fuPicture.FileName;
                fuPicture.SaveAs(file_post_url);
                e.Values["HinhAnh"] = fuPicture.FileName;
            }
            else
            {
                //Them code cho nay
            }
        }    
    }

    protected void Inserted(object sender, ListViewInsertedEventArgs e)
    {
        if (e.Exception != null)
        {
            Session["msg_type"] = "error";
            Session["msg"] = "Đã có lỗi trong quá trình thêm tài liệu!  Vui lòng thử lại!";
        }
        else
        {
            Session["msg_type"] = "success";
            Session["msg"] = "Đã hoàn tất thêm tài liệu!";
        }
    }

    private bool CheckFileType(string FileName)
    {
        string[] ext =  FileName.Split('.');
        ext[1] = ext[1].ToLower();
        if (ext[1].Equals("png") || ext[1].Equals("gif") || ext[1].Equals("jpg") || ext[1].Equals("jpeg"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    protected void To_DanhSach(object sender, EventArgs e)
    {
        Response.Redirect("QuanLySanPham.aspx");
    }

}